package impl;

import anaydis.sort.Sorter;
import anaydis.sort.SorterType;
import anaydis.sort.gui.ObservableSorter;
import anaydis.sort.gui.SorterListener;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;


public abstract class AbstractSorter implements Sorter, ObservableSorter {
    private SorterType sorterType;
    private List<SorterListener> sorterListeners;


    protected AbstractSorter(SorterType sorterType) {

        this.sorterType=sorterType;
        sorterListeners = new ArrayList<SorterListener>();

    }

    public <T> void sort(Comparator<T> tComparator, List<T> ts) {
    }

    public SorterType getType() {
        return sorterType;
    }

    protected <T> boolean greater(Comparator<T> c, List<T> list, int i, int j) {
        for (SorterListener sorterListener : sorterListeners) {
            sorterListener.greater(i, j);
        }

        return c.compare(list.get(i), list.get(j)) > 0;
    }

    protected <T> boolean equals(Comparator<T> c, List<T> list, int i, int j) {
        for (SorterListener sorterListener : sorterListeners) {
            sorterListener.equals(i, j);
        }
        return c.compare(list.get(i), list.get(j)) == 0;
    }

    protected <T> void swap(List<T> list, int i, int j) {
        for (SorterListener sorterListener : sorterListeners) {
            sorterListener.swap(i, j);
        }
        T aux = list.set(i, list.get(j));
        list.set(j, aux);

    }

    public void addSorterListener(SorterListener sorterListener) {

        sorterListeners.add(sorterListener);
    }

    public void removeSorterListener(SorterListener sorterListener) {

        sorterListeners.remove(sorterListener);
    }

}
